package com.qkst.website.web.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.backstage.bean.ProductBean;
import com.qkst.website.common.core.BaseController;
import com.qkst.website.common.core.Pager;
import com.qkst.website.common.utils.SetEnums;
import com.qkst.website.web.bean.BargainDetailBean;
import com.qkst.website.web.bean.BargainLogBean;
import com.qkst.website.web.bean.UserBean;
import com.qkst.website.web.service.BargainDetailService;
import com.qkst.website.web.service.BargainService;
import com.qkst.website.web.service.ProductService;

/**
 * @author lihao
 * @description 砍价控制器
 * @date 2017-05-08
 * @email ahlxlihao@163.com
 *
 */
@Controller
@RequestMapping("/web/bargain")
public class WebBargainController extends BaseController{


	@Autowired
	private BargainService bargainService;
	
	@Autowired
	private BargainDetailService bargainDetailService;
	
	@Autowired
	private ProductService productService;
	
	/**
	 * 砍价记录列表页面数据查找
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list.html", method = { RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,BargainLogBean bargainLog) throws IOException{
		ModelAndView mv= this.webModelAndView("bargain/list");
		bargainLog.setPage(1);
		bargainLog.setRows(10);
		Pager pager = new Pager();
		UserBean loginUser=(UserBean) request.getSession().getAttribute(SetEnums._LOGIN_USER.toString());
		bargainLog.setBuyer(loginUser);
		bargainLog.setBuyerId(loginUser.getUserId());//设置当前登陆用户
		//分页查询砍价列表
		List<BargainLogBean> bargainLogList=bargainService.queryBargainLogList(bargainLog);
		//砍价总记录数
		int records=bargainService.queryCount(bargainLog);
		pager.setRecords(records);
		mv.addObject("bargainLogList", bargainLogList);//返回砍价列表
		mv.addObject("pager", pager);//返回总记录数据
		return mv;
	}
	
	/**创建砍价URL
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/createBargainURL.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> createBargainLog(HttpServletRequest request,HttpServletResponse response,@RequestBody BargainLogBean bargainLog) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		UserBean user =(UserBean) request.getSession().getAttribute(SetEnums._LOGIN_USER.toString());
		bargainLog.setBuyer(user);
		bargainLog.setBuyerId(user.getUserId());
		//保存砍价记录
		bargainService.save(bargainLog,result);
		return result;
	}
	
	/**删除砍价记录
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/delete.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request,HttpServletResponse response,BargainLogBean bargainLog) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//取消砍价记录
		bargainService.delete(bargainLog,result);
		return result;
	}
	
	/**邀请砍价
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/inviteBargain.do", method = { RequestMethod.GET })
	public ModelAndView inviteBargain(HttpServletRequest request,HttpServletResponse response,String partnum) throws IOException{
		ModelAndView mv= this.webModelAndView("bargain/inviteBargain");
		//查询产品根据编号
		ProductBean product=productService.queryProductByPartnum(partnum);
		Map<String, Object> result = new HashMap<String, Object>();
		UserBean user =this.getLoginUser(request, response);//获取登陆用户
		BargainLogBean bargainLog=new BargainLogBean();
		bargainLog.setBuyer(user);
		bargainLog.setBuyerId(user.getUserId());
		bargainLog.setProduct(product);
		//检查是否是第一次
		
		bargainLog = bargainService.queryBargainLogByConditions(bargainLog);
		if(bargainLog!=null && bargainLog.getBargainId()!=null){//不是第一次邀请砍价
			mv.addObject("bargainLog",bargainLog);//返回砍价信息
		}else{//是第一次邀请砍价 生成token
			//保存砍价记录
			bargainLog=new BargainLogBean();
			bargainLog.setBuyer(user);
			bargainLog.setBuyerId(user.getUserId());
			bargainLog.setProduct(product);
			bargainService.save(bargainLog,result);
			if("00".equals(result.get("code"))){//如果成功
				mv.addObject("bargainLog", result.get("bargainLog"));//返回砍价信息
				mv.addObject("code", result.get("code"));//返回编码
			}else{//失败
				mv.addObject("code",result.get("code"));//返回编码
				mv.addObject("bargainLog", result.get("bargainLog"));//返回砍价信息
			}
		}
		
		
		mv.addObject("product", product);//返回产品
		return mv;
	}
	
	/**去砍价
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/goToBargain.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> goToBargain(HttpServletRequest request,HttpServletResponse response,String token) throws Exception{
		Map<String, Object> result = new HashMap<String, Object>();
		if(request.getSession().getAttribute(SetEnums._LOGIN_USER.toString())!=null ){//如果已登陆
			UserBean user = (UserBean) request.getSession().getAttribute(SetEnums._LOGIN_USER.toString());
			BargainDetailBean bargainDetail = new BargainDetailBean(); 
			BargainLogBean bargainLog = new BargainLogBean(); 
			bargainLog.setToken(token);//验证token
			bargainDetail.setBargainLog(bargainLog);
			bargainDetail.setUser(user);//设置砍价用户
			bargainDetail.setUserId(user.getUserId());
			bargainDetailService.save(bargainDetail,result);
		}else{
			result.put("msg","亲,对不起,砍价失败!记得要登陆后击链接才能成功砍价噢!");
			result.put("isSuccess",false);
			result.put("code","405");//未登录
		}
		return result;
	}
	
	/**帮忙去砍价
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/helpToBargain.html", method = { RequestMethod.GET })
	public ModelAndView helpToBargain(HttpServletRequest request,HttpServletResponse response,@RequestParam String token) throws Exception{
		ModelAndView mv= this.webModelAndView("bargain/helpBargain");
		BargainLogBean bargainLog = new BargainLogBean();
		bargainLog.setToken(token);
		//根据token查询砍价
		bargainLog=bargainService.queryBargainLogByConditions(bargainLog);
		mv.addObject("bargainLog", bargainLog);//返回砍价列表
		return mv;
	}

}
